public class code2 {
    public int maxTurbulenceSize(int[] nums) {
        int n = nums.length;

        int[] f = new int[n];//上升
        int[] g = new int[n];//下降

        for(int i=0;i<n;i++) {
            f[i] = g[i] = 1;
        }

        int ret = 1;
        for(int i=1;i<n;i++) {
            if(nums[i-1]<nums[i])
                f[i] = g[i-1] + 1;
            else if(nums[i-1]>nums[i])
                g[i] = f[i-1] + 1;
            ret = Math.max(f[i],Math.max(g[i],ret));
        }

        return ret;
    }
}
